package com.ws.admin.config;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.text.SimpleDateFormat;
import java.util.List;

@Configuration
public class MyWebMvcConfigurerAdapter extends WebMvcConfigurationSupport {

    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        //registry.addInterceptor(new MvcInterceptor()).addPathPatterns("/**").excludePathPatterns("/static/**");;

        super.addInterceptors(registry);
    }
    //添加转换器
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        //将我们定义的时间格式转换器添加到转换器列表中,
        //这样jackson格式化时候但凡遇到Date类型就会转换成我们定义的格式
        converters.add(jackson2HttpMessageConverter());

        // 配置返回JSONMappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        ////        ObjectMapper objectMapper = new ObjectMapper();
        ////        SimpleModule module = new SimpleModule();
        ////        module.addSerializer(new ToStringSerializer(Long.TYPE));
        ////        module.addSerializer(new ToStringSerializer(Long.class));
        ////        module.addSerializer(new ToStringSerializer(BigInteger.class));
        ////        objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() {
        ////            @Override
        ////            public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        ////                jsonGenerator.writeString("");
        ////            }
        ////        });
        ////        objectMapper.registerModule(module);
        ////        converter.setObjectMapper(objectMapper);
        ////
        ////        //这里是fastJSON的配置方式，更多的内容可以查看SerializerFeature
        //////        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        //////        converter.setFeatures(SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteNullNumberAsZero,
        //////                SerializerFeature.WriteNullBooleanAsFalse, SerializerFeature.WriteNullListAsEmpty);
        ////        converters.add(converter);
    }

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        /* 继承了WebMvcConfigurationSupport之后 必须重写*/
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");

        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        super.addResourceHandlers(registry);
    }

    //日期发送到前端格式化
    @Bean
    public MappingJackson2HttpMessageConverter jackson2HttpMessageConverter() {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        converter.setObjectMapper(mapper);
        return converter;
    }

    //日期接收格式化
    @Override
    protected void addFormatters(FormatterRegistry registry) {
        registry.addFormatter(new DateFormatter("yyyy-MM-dd HH:mm:ss"));
        super.addFormatters(registry);
    }
}
